//
//  YZXEmoticonModel.swift
//  表情键盘
//
//  Created by 叶周翔 on 16/7/29.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

class YZXEmoticonModel: NSObject {
    /// 表情所在的文件夹
    var id: String
    
 /// 表情名称
    var chs: String?
 /// 表情图片名称
    var png: String?{
        didSet {
            fullPngPath = YZXEmojiManager.shareManager.bundlePath + "/" + id  + "/" + png!
        }
    }
    /// 图片的完整路径
    var fullPngPath: String?
    
    /// emoji 16 进制字符串
    var code: String? {
        didSet {
            // 扫描
            let sacnner = NSScanner(string: code!)
            
            var result: UInt32 = 0
            // UnsafeMutablePointer<UInt32>: 要一个UInt32类型的指针
            sacnner.scanHexInt(&result)
            
            let scalar = UnicodeScalar(result)
            
            let c = Character(scalar)
            
            // 转成emoji
            emoji = String(c)
        }
    }
    
    /// emoji
    var emoji: String?
    
    init(id:String,dict: [String: String]) {
        self.id = id
        super.init()
        //KVC
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    // MARK: - 归档和解档
    required init?(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObjectForKey("id") as? String ?? ""
        chs = aDecoder.decodeObjectForKey("chs") as? String
        png = aDecoder.decodeObjectForKey("png") as? String
        
        code = aDecoder.decodeObjectForKey("code") as? String
        emoji = aDecoder.decodeObjectForKey("emoji") as? String
        if png != nil {
            fullPngPath = YZXEmojiManager.shareManager.bundlePath + "/" + id  + "/" + png!
        }
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
        
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(emoji, forKey: "emoji")
    }

    
}
